package com.rongc.client.freight.base.view.activity;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AutoCompleteTextView;

import com.amap.api.services.core.AMapException;
import com.amap.api.services.help.Inputtips;
import com.amap.api.services.help.InputtipsQuery;
import com.amap.api.services.help.Tip;
import com.rongc.client.core.utils.ActivityUtils;
import com.rongc.client.core.utils.StringUtils;
import com.rongc.client.freight.R;
import com.rongc.client.freight.base.BaseActivity;
import com.rongc.client.freight.base.model.TipListBean;
import com.rongc.client.freight.base.model.TipTemp;
import com.rongc.client.freight.base.view.adapter.RecyclerBaseAdapter;
import com.rongc.client.freight.base.view.adapter.RecyclerMapAdapter;
import com.rongc.client.freight.base.view.widget.DividerItemDecoration;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by Bruce on 2017/7/6.
 */
public class MapGdActivity extends BaseActivity implements TextWatcher, Inputtips.InputtipsListener,View.OnClickListener {
    private String city = "";
    @Bind(R.id.input_edittext)
    AutoCompleteTextView mKeywordText;
    @Bind(R.id.recycler)
    RecyclerView mRecyclerView;

    RecyclerMapAdapter aAdapter;
    List<TipTemp> datas;

    @OnClick({R.id.iv_back})
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_back:
                onBackPressed();
            break;
        }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count,
                                  int after) {
        // TODO Auto-generated method stub

    }

    @Override
    public void afterTextChanged(Editable s) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        String newText = s.toString().trim();

        if(StringUtils.isNotEmpty(newText)) {
            InputtipsQuery inputquery = new InputtipsQuery(newText, city);
            inputquery.setCityLimit(true);
            Inputtips inputTips = new Inputtips(MapGdActivity.this, inputquery);
            inputTips.setInputtipsListener(this);
            inputTips.requestInputtipsAsyn();
        }else{
            TipListBean bean = TipListBean.load();
            if(bean != null){
                datas.clear();
                datas.addAll(bean.getTips());
                datas.add(new TipTemp());
            }

            aAdapter.notifyDataSetChanged();
        }

    }

    @Override
    public void onGetInputtips(final List<Tip> tipList, int rCode) {
        if (rCode == AMapException.CODE_AMAP_SUCCESS) {
            datas.clear();
            if(tipList != null){
                for (Tip tip: tipList) {
                    datas.add(new TipTemp(tip));
                }
            }
            aAdapter.notifyDataSetChanged();

        } else {
            ActivityUtils.toast("rCode");
        }

    }

    @Override
    public void initView() {
        TipListBean bean = TipListBean.load();
        if(bean != null){
            datas = bean.getTips();
            datas.add(new TipTemp());
        }

        if(datas == null)
            datas = new ArrayList<>();

        aAdapter = new RecyclerMapAdapter(this,datas);

        mRecyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
        mRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST));
        mRecyclerView.setAdapter(aAdapter);
        aAdapter.setOnItemClickListener(new RecyclerBaseAdapter.OnItemClickListener<TipTemp>() {
            @Override
            public void onItemClick(View view, int position, TipTemp data) {
                if("-1".equals(data.getPoiID())){
                    datas.clear();
                    aAdapter.notifyDataSetChanged();
                    TipListBean.clear();
                }else {
                    TipListBean bean = TipListBean.load();
                    if (bean == null) {
                        bean = new TipListBean();
                    }
                    bean.add(data);
                    bean.save();

                    Intent intent = new Intent();
                    intent.putExtra("data", data);
                    setResult(RESULT_OK, intent);
                    finish();
                }
            }
        });

        mKeywordText.addTextChangedListener(this);
    }

    @Override
    public int getLayoutId() {
        return R.layout.universal_map_gd;
    }
}
